vpackage com.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

/**
 * Created by Administrator on 2017/10/16.
 */

public class MyProvider  extends ContentProvider{


    public static final int BOOK_DIR = 0;
    public static final int BOOK_ITEM = 1;
    public static final int CATEGORY_DIR = 2;
    public static final int CATEGORY_ITEM = 3;
    public static final String AUTHORITY = "com.provider";
    private static UriMatcher urimatcher;
    private MyDataBaseHelper  dbhelper;

    static{

        urimatcher = new UriMatcher(UriMatcher.NO_MATCH);
        urimatcher.addURI(AUTHORITY,"book",BOOK_DIR);
        urimatcher.addURI(AUTHORITY,"book/#",BOOK_ITEM);
        urimatcher.addURI(AUTHORITY,"category",CATEGORY_DIR);
        urimatcher.addURI(AUTHORITY,"category/#",CATEGORY_ITEM);
    }





    @Override
    public boolean onCreate() {
        dbhelper = new MyDataBaseHelper(getContext(),"BookStore.db",null,1);
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {

        SQLiteDatabase db = dbhelper.getReadableDatabase();

        Cursor cursor = null;
        switch (urimatcher.match(uri)){

            case BOOK_DIR:
                    cursor = db.query("Book",projection,selection,selectionArgs,null,null,sortOrder);

                break;
            case BOOK_ITEM:
                    String boodId = uri.getPathSegments().get(1);
                    db.query("Book",projection,"id=?",new String[]{boodId},null,null,sortOrder);
                break;
            case CATEGORY_DIR:
                cursor = db.query("Category",projection,selection,selectionArgs,null,null,sortOrder);
                break;
            case CATEGORY_ITEM:
                String categoryId = uri.getPathSegments().get(1);
                db.query("Category",projection,"id=?",new String[]{categoryId},null,null,sortOrder);
                break;
            default:
                break;


        }



        return cursor;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        switch (urimatcher.match(uri)){

            case  BOOK_DIR:
                return "vnd.android.cursor.dir/vnd.com.provider.book";

            case BOOK_ITEM:
                return "vnd.android.cursor.item/vnd.com.provider.book";


            case CATEGORY_DIR:
                return "vnd.android.cursor.dir/vnd.com.provider.category";

            case CATEGORY_ITEM:
                return "vnd.android.cursor.item/vnd.com.provider.category";


            default:

                break;


        }

        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {

        SQLiteDatabase db = dbhelper.getWritableDatabase();
        Uri uriReturn = null;
        switch (urimatcher.match(uri)){

            case BOOK_DIR:
            case BOOK_ITEM:
                 long newBookId = db.insert("Book",null,values);
                 uriReturn = Uri.parse("content://"+AUTHORITY+"/book/"+newBookId);


                break;
            case CATEGORY_DIR:
            case CATEGORY_ITEM:
                long categoryId = db.insert("Category",null,values);
                uriReturn = Uri.parse("content://"+AUTHORITY+"/category/"+categoryId);
                break;

        }

        return uriReturn;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        SQLiteDatabase db = dbhelper.getWritableDatabase();
        int deletedRows = 0;
        switch (urimatcher.match(uri)){

            case BOOK_DIR:
                deletedRows = db.delete("Book",selection,selectionArgs);

                break;
            case BOOK_ITEM:
                String bookId = uri.getPathSegments().get(1);
                deletedRows = db.delete("Book","id=?",new String[]{bookId});
                break;

            case CATEGORY_DIR:
                deletedRows = db.delete("Category",selection,selectionArgs);
                break;

            case CATEGORY_ITEM:
                String categoryId = uri.getPathSegments().get(1);
                deletedRows = db.delete("Category","id=?",new String[]{categoryId});
                break;



        }


        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        SQLiteDatabase db = dbhelper.getWritableDatabase();
        int updatedRows = 0;
        switch (urimatcher.match(uri)){

            case BOOK_DIR:
                    updatedRows = db.update("Book",values,selection,selectionArgs);
                break;
            case BOOK_ITEM:
                    String bookId = uri.getPathSegments().get(1);
                    updatedRows = db.update("Book",values,"id=?",new String[]{bookId});
                break;

            case CATEGORY_DIR:
                updatedRows = db.update("Category",values,selection,selectionArgs);
                break;

            case CATEGORY_ITEM:
                String categoryId = uri.getPathSegments().get(1);
                updatedRows = db.update("Category",values,"id=?",new String[]{categoryId});
                break;
        }


        return 0;
    }
}
//////////////////////////////////
 <provider
            android:authorities="com.provider"
            android:name=".MyProvider"
            android:enabled="true"
            android:exported="true"
            />
			
利用第三方实例
add:
uri = Uri.parse("content://com.provider/book");
                values = new ContentValues();
                values.put("name","add");
                values.put("author","bob");
                values.put("pages",33);
                values.put("price",22.22);
                Uri newUri = getContentResolver().insert(uri,values);
                newId = newUri.getPathSegments().get(1);
				
delete:
uri = Uri.parse("content://com.provider/book/"+newId);
                getContentResolver().delete(uri,null,null);
				
select:
uri = Uri.parse("content://com.provider/book");

                Cursor cursor = getContentResolver().query(uri,null,null,null,null);
                if(cursor.moveToFirst()){

                    do{

                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        String author = cursor.getString(cursor.getColumnIndex("author"));
                        String page = cursor.getString(cursor.getColumnIndex("pages"));

                        Log.i("sql select",name+" "+author+" "+page+"\n");

                    }while(cursor.moveToNext());
                }
update:
 uri = Uri.parse("content://com.provider/book/"+newId);
                values = new ContentValues();
                values.put("price","123");
                getContentResolver().update(uri,values,null,null);